<?php
////////////////////////////////////////////////////
//	LinBoard
//	Framework/User.php
//	Created: 20th February 2009; 21:21 UTC
//	LinBoard (c) Copyright OpenInferno 2008, 2009
////////////////////////////////////////////////////

//Setup Environment
error_reporting(E_ALL | E_STRICT);

//Direct Access Check
if(!defined("LINBOARD"))
{
	die("<h1>Access Error</h1>Direct Access to this script is forbidden.");
}

class User
{
	
	public $currentUser = '';
	
	public function __construct()
	{
		$this->currentUser = $this->getUserDetails();
	}
	
	public function getUserDetails($uid = null)
	{
		Framework::Mysql()->requireConnection();
		
		//Guest Details
		$GuestDetails = array();
		
		$GuestDetails['uid'] = 0;
		$GuestDetails['username'] = "Guest";
		$GuestDetails['cleanusername'] = "guest";
		$GuestDetails['password'] = "";
		$GuestDetails['salt'] = "";
		$GuestDetails['usergroup'] = -1;
		$GuestDetails['totalposts'] = 0;
		$GuestDetails['location'] = "";
		$GuestDetails['email'] = "";
		$GuestDetails['gender'] = -1;
		$GuestDetails['usertitle'] = "";
		$GuestDetails['IM'] = "";
		$GuestDetails['signature'] = "";
		
		if($this->currentUser != '' && $uid == null)
		{
			return $this->currentUser;
		}
		
		if($uid == null)
		{
			if(isset($_COOKIE['username']))
			{
				$username = Framework::Misc()->cleanUsername($_COOKIE['username']);
				$result = Framework::Mysql()->query("SELECT * FROM `".Framework::Config()->Mysql['prefix']."users` WHERE `cleanusername` = '{$username}' LIMIT 1;");
			} else {
				return $GuestDetails;
			}
			
			if(Framework::Mysql()->numRows($result) < 1)
			{
				return $GuestDetails;
			}
			
			$User = array();
			$row = Framework::Mysql()->fetchArray($result);
		
			$User['uid'] = $row['uid'];
			$User['username'] = $row['username'];
			$User['cleanusername'] = $row['cleanusername'];
			$User['password'] = $row['password'];
			$User['salt'] = $row['salt'];
			$User['usergroup'] = $row['usergroup'];
			$User['totalposts'] = $row['totalposts'];
			$User['location'] = $row['location'];
			$User['email'] = $row['email'];
			$User['gender'] = $row['gender'];
			$User['usertitle'] = $row['usertitle'];
			$User['IM'] = $row['IM'];
			$User['signature'] = $row['signature'];
			
			return $User;
		}
		
		if(isset($uid) && is_numeric($uid))
		{
			$uid = (int)$uid;
		}
		
		$result = Framework::Mysql()->query("SELECT * FROM `".Framework::Config()->Mysql['prefix']."users` WHERE `uid` = '{$uid}' LIMIT 1;");
		
		if(Framework::Mysql()->numRows($result) < 1)
		{
			return false;
		}
		
		$User = array();
		$row = Framework::Mysql()->fetchArray($result);
		
		$User['uid'] = $row['uid'];
		$User['username'] = $row['username'];
		$User['cleanusername'] = $row['cleanusername'];
		$User['password'] = $row['password'];
		$User['salt'] = $row['salt'];
		$User['usergroup'] = $row['usergroup'];
		$User['totalposts'] = $row['totalposts'];
		$User['location'] = $row['location'];
		$User['email'] = $row['email'];
		$User['gender'] = $row['gender'];
		$User['usertitle'] = $row['usertitle'];
		$User['IM'] = $row['IM'];
		$User['signature'] = $row['signature'];
		
		return $User;
	}
	
	public function Group($user = 0)
	{
		Framework::Mysql()->requireConnection();
		
		$user = (int)$user;
		
		if($user == (0 || null))
		{
			$user = $this->getUserDetails();
			$user = (int)$user['uid'];
		}
		
		
		
		$result = Framework::Mysql()->query("SELECT * FROM `".Framework::Config()->Mysql['prefix']."group` WHERE `gid` = '{$user}' LIMIT 1;");
		
		$group = array();
		while($row = Framework::Mysql()->fetchArray($result))
		{
				$group['gid'][] = $row['gid'];
				$group['gname'][] = $row['gname'];
				$group['utitle'][] = $row['utitle'];
				$group['global_permissions'][] = $row['global_permissions'];
		}
		
		return $group;
	}
	
	public static function getInstance()
	{
		static $instance;
		if(!isset($instance))
		{
			$class = __CLASS__;
			$instance = new $class;
		}
		return $instance;
	}
	
}

?>